package com.spc.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 
 * �������� <b>Redis�ͻ���</b>
 * �޸ı�ע�� 
 * @version
 */
@SuppressWarnings("deprecation")
public class JedisUtil {
	private static Logger logger = LoggerFactory.getLogger(JedisUtil.class);
	//���ӳض���
	private static JedisPool jedisPool;

	static {
		String host = PropUtil.getString("redis.host");
		int port = PropUtil.getInt("redis.port");
		int timeout=PropUtil.getInt("redis.timeout");
		String password=PropUtil.getString("redis.password");
		String database=PropUtil.getString("redis.database");
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(PropUtil.getInt("redis.maxIdle"));
        config.setMinIdle(PropUtil.getInt("redis.minIdle"));
        config.setMaxTotal(PropUtil.getInt("redis.maxActive"));
        config.setMaxWaitMillis(PropUtil.getInt("redis.maxWait"));
        config.setTestOnBorrow(PropUtil.getBoolean("redis.testOnBorrow",true));
        config.setTestOnReturn(true);
        config.setTestWhileIdle(true);
        config.setMinEvictableIdleTimeMillis(60000l);
        config.setTimeBetweenEvictionRunsMillis(3000l);
        config.setNumTestsPerEvictionRun(-1);
        if(!Tools.isNotNull(password)){
        	jedisPool = new JedisPool(config, host, port);
        }else{
        	jedisPool = new JedisPool(config, host, port,timeout,password,Integer.parseInt(database));
        }
        
	}

	/**
	 * ��ȡJedis���ӿͻ���
	 * 
	 * @return
	 */
	public static Jedis getJedisClient() {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
		} catch (Exception e) {
			logger.error("��ȡRedis�ͻ�������ʧ�ܡ�"+e);
		}
		if (jedis == null) {
			logger.warn("û�л�ȡ��Redis�ͻ������ӡ�");
		}
		return jedis;
	}

	/**
	 * ��ȫ������Դ
	 * 
	 * @param jedis
	 */
	public static void close(Jedis jedis) {
		try {
			jedisPool.returnResource(jedis);
		} catch (Exception e) {
			if (jedis.isConnected()) {
				jedis.quit();
				jedis.disconnect();
			}
		}
	}

	/**
	 * �����ַ���������
	 * 
	 * @param key
	 *            �洢��
	 * @param value
	 *            �洢ֵ
	 * @param timeout
	 *            ��ʱʱ��(��λ���룩 ����Ϊ0������ʱЧ�ԡ�
	 * @return
	 */
	public static void setString(String key, String value, int timeout) {
		if (!Tools.isNotNull(key)) {
			throw new NullPointerException("Key����Ϊ��!");
		}
		Jedis jedis = null;
		try {
			jedis = getJedisClient();
			jedis.set(key, value);
			if (timeout > 0) {
				jedis.expire(key, timeout);
			}
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
			logger.error("����Redisʧ��", e);
		} finally {
			close(jedis);
		}
	}
	
	/**
	 * �����ַ��������ݹ���ʱ��
	 * 
	 * @param key
	 *            �洢��
	 * @param timeout
	 *            ��ʱʱ��(��λ���룩
	 * @param key
	 */
	public static void exprString(String key, int timeout) {
		if (!Tools.isNotNull(key)) {
			return;
		}
		Jedis jedis = null;
		try {
			jedis = getJedisClient();
			jedis.expire(key, timeout);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
			logger.error("����Redisʧ��", e);
		} finally {
			close(jedis);
		}
	}

	/**
	 * �������л���������
	 * 
	 * @param key
	 *            �洢��
	 * @param value
	 *            �洢ֵ
	 * @param timeout
	 *            ��ʱʱ��(��λ���룩 ����Ϊ0������ʱЧ�ԡ�
	 * @return
	 */
	public static void setObj(String key, byte[] value, int timeout) {
		if (!Tools.isNotNull(key)) {
			throw new NullPointerException("Key����Ϊ��!");
		}
		Jedis jedis = null;
		try {
			jedis = getJedisClient();
			jedis.set(key.getBytes(), value);
			if (timeout > 0) {
				jedis.expire(key, timeout);
			}
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
			logger.error("����Redisʧ��", e);
		} finally {
			close(jedis);
		}
	}

	/**
	 * ��ȡ�ַ���������
	 * 
	 * @param key
	 * @return
	 */
	public static String getString(String key,String defaultValue) {
		if (!Tools.isNotNull(key)) {
			throw new NullPointerException("Key����Ϊ��!");
		}
		String value = null;
		Jedis jedis = null;
		try {
			jedis = getJedisClient();
			value = jedis.get(key);
			if(value==null){
				return defaultValue;
			}
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
			logger.error("����Redisʧ��", e);
		} finally {
			close(jedis);
		}
		return value;
	}

	/**
	 * ��ȡ���л���������
	 * 
	 * @param key
	 * @return
	 */
	public static byte[] getObj(String key) {
		if (!Tools.isNotNull(key)) {
			throw new NullPointerException("Key����Ϊ��!");
		}
		byte[] value = null;
		Jedis jedis = null;
		try {
			jedis = getJedisClient();
			value = jedis.get(key.getBytes());
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
			logger.error("����Redisʧ��", e);
		} finally {
			close(jedis);
		}
		return value;
	}

	/**
	 * ɾ����������
	 * 
	 * @param key
	 */
	public static void delObj(String key) {
		if (!Tools.isNotNull(key)) {
			return;
		}
		Jedis jedis = null;
		try {
			jedis = getJedisClient();
			jedis.del(key.getBytes());
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
			logger.error("����Redisʧ��", e);
		} finally {
			close(jedis);
		}
	}

	/**
	 * ɾ���ַ�������
	 * 
	 * @param key
	 */
	public static void delString(String key) {
		if (!Tools.isNotNull(key)) {
			return;
		}
		Jedis jedis = null;
		try {
			jedis = getJedisClient();
			jedis.del(key);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
			logger.error("����Redisʧ��", e);
		} finally {
			close(jedis);
		}
	}

	/**
	 * ���DB
	 * 
	 * @param key
	 */
	public static void flushDB() {
		Jedis jedis = null;
		try {
			jedis = getJedisClient();
			jedis.flushDB();
			logger.info("Redsi����DB���óɹ���");
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
			logger.error("����Redisʧ��", e);
		} finally {
			close(jedis);
		}
	}

   /**
    * 
    * ��Ҫ˵�����ж�redis �Ƿ��� false ������
    * ��д�ߣ�����Ԫ
    * ����ʱ�䣺2016��12��12�� ����11:47:39
    * @param ˵��
    * @return ˵��
    */
	public static  boolean  isLive() {
		Jedis jedis = null;
		try {
			jedis = getJedisClient();
			if(jedis!=null){
				String ping=jedis.ping();
				if("PONG".equals(ping)){
					return true;
				}
			}
		
		
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
			logger.error("����Redisʧ��", e);
		} finally {
			close(jedis);
		}
		return false;
	}
}
